<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 * Trả về $_REQUEST[$name], hoặc $default_value nếu biến không tồn tại
 * @param type $name
 * @param type $default_value
 * @return type
 */
function request($name, $default_value = '') {
	if (isset($_REQUEST[$name]))
		return $_REQUEST[$name];
	return $default_value;
}

/**
 * Lọc lấy dữ liệu từ mảng $_REQUEST với các khóa trong $data
 * @param type $filter
 * @return type
 */
function request_filter($filter, $data = array()) {
	$CI = &get_instance();
	$result = array();
	if (count($data) == 0) {

		foreach ($filter as $key) {
			if ($CI->input->post($key) !== FALSE)
				$result[$key] = $CI->input->post($key);
			else if($CI->input->get($key) !==FALSE)
				$result[$key] = $CI->input->get($key);
			else $result[$key]= '';
		}
	}else {
		foreach ($filter as $key) {
			$result[$key] = isset($data[$key]) ? $data[$key] : '';
		}
	}
	return $result;
}

/**
 * Lọc dữ liệu trong mảng
 */
function params_filter($data, $params) {
	$result = array();
	foreach ($params as $param) {
		$result[$param] = $data[$param];
	}
	return $result;
}

/**
 * Lấy lỗi form 
 * @param type $data
 * @return type
 */
function form_error_message($data) {
	$CI = &get_instance();
	$CI->load->helper('form');
	$error = '';
	foreach ($data as $key => $val) {
		$str = form_error($key);
		if ($str != '')
			$error .= $str . "\n";
	}
	$error = str_replace('<p>', '', $error);
	$error = str_replace('</p>', '', $error);
	return $error;
}

/**
 * Tạo prefix cho các object để sử dụng kiểu cây
 * @param type $object
 * @param type $prefix
 */
function object_prefix_create(&$object_array, $current_prefix, $prefix, $root = true) {
	foreach ($object_array as &$object) {
		if ($root && $object->p_id != 0)
			continue;
		$object->pre = $current_prefix;
		object_prefix_create($object->children, $object->pre . $prefix, $prefix, false);
	}
}

/**
 * 
 * @param array $array
 * @param type $object
 */
function object_to_array(&$array, $object_array, $root = true) {
	foreach ($object_array as $object) {
		if ($root && $object->p_id != 0)
			continue;
		$temp = $object->children;
		unset($object->children);

		$array[] = $object;
		object_to_array($array, $temp, false);
	}
}

/**
 * Load các object con lưu vào mảng children của object hiện tại
 * @param type $object
 * @param type $objects
 */
function load_children(&$object, $objects) {
	$object->children = array();
	foreach ($objects as $m)
		if ($m->p_id == $object->id) {
			load_children($m, $objects);
			$object->children[] = $m;
		}
}

/**
 * Chuyen 
 * @param type $objects
 * @param type $root_id
 */
function array_to_object(&$objects, $root_id = 0) {
	foreach ($objects as &$object)
		load_children($object, $objects);

	foreach ($objects as $key => $o)
		if ($o->p_id != $root_id)
			unset($objects[$key]);
}

/**
 * Kiem tra 1 module co phai la module con cua 1 module hay ko
 * @param type $object
 * @param type $object_check
 * @param type $objects
 * @return boolean
 */
function is_child_object($object, $object_check) {
	if ($object->id == $object_check->id)
		return true;
	foreach ($object->children as $child)
		if (is_child_object($child, $object_check))
			return true;
	return false;
}

/**
 * Load cac module co' the chua' module hien tai 
 * @param type $object
 * @param type $objects
 * @return type
 */
function delete_children($object, &$objects) {
	foreach ($objects as $key => &$m)
		if ($m->id == $object->id)
			unset($objects[$key]);
		else
			delete_children($object, $m->children);
}

/**
 * Tao breadcrumb
 * @param type $objects
 * @param type $current_id
 * @return type
 */
function breadcrumb_create($objects, $current_id) {
	$breadcrumb = array();
	if (!($current_id === FALSE)) {
		$id = $current_id;
		while ($objects[$id]->p_id != 0) {
			$id = $objects[$id]->p_id;
			$breadcrumb[] = $id;
		}
	}
	$breadcrumb = array_reverse($breadcrumb);
	return $breadcrumb;
}

/**
 * Lấy config trong bang config
 * @return type
 */
function uconfig_get() {
	$CI = &get_instance();
	$CI->load->model('model_config');

	$configs = $CI->model_config->get_list();

	$result = new stdClass();
	foreach ($configs as $config) {
		$result->{$config->name} = $config->value;
	}
	return $result;
}

/**
 * Phân trang 
 * @param type $total
 * @param type $curr
 * @return type
 */
function pagination_init($total, $curr, $item_per_page = '') {
	if (!is_numeric($item_per_page) && $item_per_page < 1) {
		$CI = &get_instance();
		$item_per_page = $CI->config->item('item_per_page');
	}

	// Kiem tra gioi han du lieu
	if (!is_numeric($curr))
		$curr = 1;
	if ($curr > $total)
		$curr = $total;
	if ($curr < 1)
		$curr = 1;
	// Lay so item/page tu config

	$obj = new stdClass();
	$obj->first = 1;
//	echo $total;die;
	$obj->last = (int) ($total / $item_per_page) + ($total % $item_per_page == 0 ? 0 : 1);
	$obj->prev = $curr == 1 ? null : $curr - 1;
	$obj->next = $curr >= $obj->last ? null : $curr + 1;
	$obj->from = ($curr - 1) * $item_per_page;
	$obj->limit = $item_per_page;

	$left = 1;
	if ($curr - 2 >= 1)
		$left = $curr - 2;

	$num = $obj->last > 5 ? 5 : $obj->last;

	if ($left + $num > $obj->last)
		$left = $obj->last - $num + 1;

	$obj->pnumbers = array();
	for ($i = 0; $i < $num; $i++)
		$obj->pnumbers[] = $left + $i;

	$obj->left = $obj->prev == null ? false : $obj->first < $obj->pnumbers[0];
	$obj->right = $obj->next == null ? false : $num > 0 ? $obj->last > $obj->pnumbers[$num - 1] : false;

	if ($obj->first == $curr)
		$obj->first = null;
	if ($obj->last == $curr)
		$obj->last = null;

	$obj->curr = $curr;

	return $obj;
}

/*
 * Lay config upload anh?
 */

function image_get_upload_config($uconfig) {
	$config = array();
	$config['encrypt_name'] = $uconfig->image_encrypt_name == 1;
	$config['upload_path'] = $uconfig->image_upload_path;
	$config['allowed_types'] = $uconfig->image_types;
	$config['max_size'] = $uconfig->image_max_size;
	$config['max_width'] = $uconfig->image_max_width;
	$config['max_height'] = $uconfig->image_max_height;
	return $config;
}

/**
 * Lay duong dan anh
 * @param $path
 * @return string
 */
function image_url($path) {
	return base_url() . $path;
}

function datetotime($str_date) {
	$ar = explode('/', $str_date);
	return mktime(0, 0, 0, $ar[1], $ar[0], $ar[2]);
}

function currency_format($num) {
	return number_format($num, 0, ',', '.');
}

?>
